﻿@{
    ViewBag.Title = "Hello";
}

<h2 id="text"> </h2>

<script type="text/javascript">
    $(document).ready(function () {
       poll();
    });

    function poll() {
        $.ajax({
            type: 'POST',
            url: 'Text',
            dataType: 'text',
            success: function (data, textStatus, xmlHttpRequest) {
                if (data && data.d && data.d.results) {
                    successCallback(data.d.results, textStatus, xmlHttpRequest);
                }
                else if (data && data.d) {
                    successCallback(data.d, textStatus, xmlHttpRequest);
                }
                else {
                    successCallback(data, textStatus, xmlHttpRequest);
                }
            },
            error: function (xmlHttpRequest, textStatus, errorThrown) {
                alert('Error in ajax call');
            }
        });
    }

    function successCallback(data, status, request) {
        if(data != '') {
            $("#text").text(data);
        }
        else {
            setTimeout(function () { }, 100);
            poll();
        }
    }
</script>
